/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pos.system.database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Shameera
 */
public class OrderSearchTable extends AbstractTableModel {

    private List<StockRowData> results;
    List<String[]> orderData = new ArrayList<String[]>();
//    String[] columnNames = new String[]{"Code", "Item Name", "Seller", "Qty", "Price"};
    String[] columnNames = new String[]{DBConstant.STOCK_CODE, DBConstant.STOCK_NAME, DBConstant.STOCK_SELLER, DBConstant.STOCK_QTY,
        DBConstant.STOCK_UNIT_PRICE};
    private ResultSet reset;

//    public OrderSearchTable(List<StockRowData> results) {
//        setOrderTable(results);
//    }
//
//    private void setOrderTable(List<StockRowData>  res) {
//        this.results = res;
//        if (results != null) {
//            for(StockRowData r : res){
//                orderData.add(new String[] {r.getCode() , r.getItemName() , r.getSeller(), r.getQty() , r.getUnit_price()});
//            }
//        } else {
//        }
//
//    }
    public OrderSearchTable(ResultSet results) {
        setTable(results);
    }

    public void setTable(ResultSet res) {
        this.reset = res;
        try {
            String[] temp;
            while (res.next()) {
                temp = new String[columnNames.length];
                for (int i = 0; i < columnNames.length; i++) {
                    if (columnNames[i].equals(DBConstant.STOCK_QTY)) {
                        String qty_s = res.getString(DBConstant.STOCK_QTY);
                        int q = Integer.valueOf(qty_s);
                        if (q <= 0) {
                            temp[i] = DBConstant.STOCK_NA;
                        } else {
                            temp[i] = qty_s;
                        }
                    } else {
                        temp[i] = (res.getString(columnNames[i]) == null ? "" : res.getString(columnNames[i]));
                    }
                }
                orderData.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(OrderSearchTable.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public int getRowCount() {
        return orderData.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return orderData.get(rowIndex)[columnIndex];
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    public void clear() {
        int size = orderData.size();
        orderData.clear();
        fireTableRowsDeleted(0, size);
    }
}
